home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Eudora 1.3.1 / source / inet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-16  |  1.8 KB  |  78 lines  |  [TEXT/MPS ]

  1. #define FILE_NUM 18
  2. /* Copyright (c) 1990-1992 by the University of Illinois Board of Trustees */
  3. #pragma load EUDORA_LOAD
  4. #pragma segment Util
  5. /**********************************************************************
  6.  * DotToNum - turn an address in dotted decimal into an internet address
  7.  * returns True if the conversion was successful.  This routine is
  8.  * somewhat limited, in that it will accept only four octets, and does
  9.  * not permit the abbreviated forms for class A and B networks.
  10.  **********************************************************************/
  11. Boolean DotToNum(UPtr string,long *nPtr)
  12. {
  13.     long address=0;
  14.     Byte b=0;
  15.     UPtr cp;
  16.     int dotcount=0;
  17.     
  18.     /*
  19.      * allow leading spaces
  20.      */
  21.     for (cp=string+1;cp<=string+*string;cp++) if (*cp!=' ') break;
  22.     
  23.     /*
  24.      * the address
  25.      */
  26.     for (;cp<=string+*string;cp++)
  27.     {
  28.         if (*cp=='.')
  29.         {
  30.             if (++dotcount > 3) return (False); /* only 4 octets allowed */
  31.             address <<= 8;
  32.             address |= b;
  33.             b=0;
  34.         }
  35.         else if (isdigit(*cp))
  36.         {
  37.             b *= 10;
  38.             b |= (*cp - '0');
  39.             if (b>255) return (False);                    /* keep it under 256 */
  40.         }
  41.         else if (*cp==' ')                                        /* allow trailing spaces */
  42.             break;
  43.         else
  44.             return (False);                                         /* periods or digits ONLY */
  45.     }
  46.     
  47.     /*
  48.      * final checks, assignment
  49.      */
  50.     if (dotcount!=3) return (False);
  51.     address <<= 8;
  52.     address |= b;
  53.     *nPtr = address;
  54.     return(True);
  55. }
  56.  
  57. /**********************************************************************
  58.  * NumToDot - turn an address into a dotted decimal string
  59.  **********************************************************************/
  60. UPtr NumToDot(unsigned long num,UPtr string)
  61. {
  62.     unsigned char* bp=(unsigned char *)#
  63.     UPtr cp=string;
  64.     int count=4;
  65.     int length;
  66.     
  67.     for (count=4;count;count--,bp++)
  68.     {
  69.         NumToString((unsigned long)*bp,cp);
  70.         length = *cp;
  71.         *cp = '.';
  72.         cp += length+1;
  73.     }
  74.     *string = cp-string-1;
  75.     return(string);
  76.  
  77.